package thread;

import java.util.*;

/**
 * 并发安全的集合API：
 * 1.ArrayList、LinkedList、HashSet都不是并发安全的（是异步的）
 */
public class SyncDemo4 {
    public static void main(String[] args) {

        List<Integer> list = new ArrayList<>();
       /*
            异步集合：
        List<Integer> list = new ArrayList<>();
        List<Integer> list = new LinkedList<>();
        Set<Integer> list = new HashSet<>();
        Set<Integer> list = new LinkedHashSet<>();
        */


         /*
            同步集合：
          List<Integer>list = Collections.synchronizedList(new ArrayList<>());
          List<Integer> list = Collections.synchronizedList(new LinkedList<>());
          Set<Integer> list = Collections.synchronizedSet(new HashSet<>());
          Set<Integer> list =Collections.synchronizedSet(new LinkedHashSet<>());
          */


        Thread t1 = new Thread(){
            public void run(){
                for (int i = 0; i < 1000; i++) {
                    list.add(i);
                }
            }
        };

        Thread t2 = new Thread(){
            public void run(){
                for (int i = 1000; i < 2000; i++) {
                    list.add(i);
                }
            }
        };

        t1.start();
        t2.start();
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(list);
    }
}
